from rest_framework import serializers
from rest_framework.viewsets import GenericViewSet,ModelViewSet
from aaa import models

# class roleSerializer(serializers.Serializer):
#     id = serializers.IntegerField()
#     ttt = serializers.CharField(source='roleName')

class roleSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Role
        fields = ['id','roleName','roleDesc']

class rolesView(ModelViewSet):
    '''订单相关业务'''
    print('1111111111111111 roles')
    queryset = models.Role.objects.all()
    serializer_class = roleSerializer
    # def list(self,request,*args,**kwargs):
    #     roles = models.Role.objects.all()
    #     print('ser.data',type(roles))
    #     ser = roleSerializer(instance=roles,many=True)
    #     print(ser.data)
    #     # ret = json.dumps(ser.data,ensure_ascii=False)
    #     # return JsonResponse({'data':ser.data,'meta':{'status':200,'msg':'角色创建成功'}})
    #     return Response(ser.data)

    # def post(self,request,*args,**kwargs):
    #     data = request.data
    #     # obj,created = models.Role.objects.update_or_create(roleName=data['roleName'])
    #     # print('created',created)
    #     try:
    #         obj = models.Role.objects.filter(roleName=data['roleName']).first()
    #         print(obj)
    #         if not obj:
    #             obj = models.Role.objects.create(roleName=data['roleName'],roleDesc=data['roleDesc'])
    #             obj.save()
    #             print(obj.id)
    #             return JsonResponse({'data':{'roleId':1},'meta':{'status':201,'msg':'角色创建成功'}})
    #         return JsonResponse({'meta': {'status': 203, 'msg': '角色已存在!'}})
    #     except Exception as e:
    #         return JsonResponse({'meta':{'status':203,'msg':'获取数据异常'}})
